<script setup lang="ts">
import { reactive } from "vue";
import { UserLoginRequest, UserControllerService } from "@/../generated";
import message from "@arco-design/web-vue/es/message";
import { useStore } from "vuex";
import { useRouter } from "vue-router";

const store = useStore();
const router = useRouter();
const form = reactive({
  userAccount: "",
  userPassword: "",
} as UserLoginRequest);
//登录功能
const handleSubmit = async (data: any) => {
  //此处不能用data，只能用form,因为data里面的属性是被封装过的，比如访问data.userAccount访问不到属性
  const res = await UserControllerService.userLoginUsingPost(data.values);
  if (res.code === 0) {
    await store.dispatch("user/getLoginUser");
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登录失败" + res.message);
  }
};
</script>

<template>
  <div style="text-align: center">
    <a-form
      style="max-width: 480px; margin: 0 auto"
      label-align="left"
      auto-label-width
      :model="form"
      @submit="handleSubmit"
    >
      <a-form-item field="userAccount" label="账号:">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item tooltip="请输入至少八位" field="userPassword" label="密码:">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" style="width: 120px" html-type="submit"
          >登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<style scoped></style>
